﻿using System.IO;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using ChannelVN.CMS.DynamicLayout.Entity;
using System;
using System.Web;

namespace ChannelVN.CMS.DynamicLayout.FrontEnd.Base
{
    public abstract class DynamicControlBase : UserControl
    {
        public FullModuleInPageWithConfigEntity ControlInfo { get; set; }
        public bool IsDesignMode { get; set; }

        /// <summary>
        /// {0} -> {ModuleName}
        /// {1} -> {ListOfSkin}
        /// {2} -> {ModuleInPageId}
        /// </summary>
//        const string ConfigHeaderFormat = @"
//                <div class=""DLModuleHeader"">
//                    {0}
//                    <div class=""ModuleFunctions"">
//                        <div class=""ModuleFunctionsContent"">
//                            <a href=""#"" class=""ModuleFunctionsShowHide"">[+]</a>
//                            <ul>
//                                {1}
//                                <li><a onclick=""DLManager.DynamicLayout.ConfigModule({2}); return false;"" href=""#"" class=""CMSDlModuleConfigBtn"" func=""Config"">Cấu hình module</a></li>
//                                <li><a href=""#"" func=""Remove"">Xóa module</a></li></ul>
//                        </div>
//                    </div>
//                </div>";

        const string ConfigHeaderFormat = @"
                        <div class=""DLModuleHeader"">
                            {0}
                            <div class=""ModuleFunctions"">
                                <div class=""ModuleFunctionsContent"">
                                    <ul class=""sf-menu module-function-menu"">
                                        <li class=""current""><a href=""#a"">+</a>
                                            <ul>
                                                <li class=""current""><a href=""#"">Chọn skin</a>
                                                    {1}
                                                </li>
                                                <li><a onclick=""DLManager.DynamicLayout.ConfigModule({2}); return false;"" href=""#"" class=""CMSDlModuleConfigBtn"" func=""Config"">Cấu hình module</a></li>
                                                <li><a href=""#"" func=""Remove"">Xóa module</a></li>
                                            </ul>
                                        </li>
                                    </ul>
                                </div>
                            </div>
                        </div>";

        /// <summary>
        /// {0} -> {ModuleInPageId}
        /// {1} -> {ModuleSkinSrc}
        /// {2} -> {ModuleSkinName}
        /// </summary>
        private const string HtmlModuleSkinItem = "<li><a onclick=\"DLManager.DynamicLayout.ChangeSkin({0},'{1}'); return false;\" href=\"#\">{2}</a></li>";

        private const string ModuleWapperCssClassName = "DLModuleItem";

        private string HeaderHtmlData { get; set; }

        protected void OnLoadDynamicControl()
        {
            if (IsDesignMode && Controls.Count > 0)
            {
                var firstControl = Controls[0];
                if (firstControl is HtmlGenericControl)
                {
                    var container = firstControl as HtmlGenericControl;

                    var moduleContainerClassName = container.Attributes["class"];
                    moduleContainerClassName = moduleContainerClassName + " " +
                                               ModuleWapperCssClassName;
                    container.Attributes["class"] = moduleContainerClassName;

                    container.Attributes.Add("moduleid", ControlInfo.Module.Id.ToString());
                    container.Attributes.Add("placeholdername", ControlInfo.PlaceHolderName);
                    container.Attributes.Add("moduleinpageid", ControlInfo.Id.ToString());
                    // -----------------------

                    // Header để config module
                    var skinData = ControlInfo.Module.ModuleSkin.Aggregate("",
                                                                           (current, skin) =>
                                                                           current +
                                                                           string.Format(
                                                                               HtmlModuleSkinItem,
                                                                               ControlInfo.Id,
                                                                               skin.SkinSrc,
                                                                               skin.SkinName));
                    if (!string.IsNullOrEmpty(skinData))
                    {
                        skinData = "<ul>" + skinData + "</ul>";
                    }
                    var configHeader = string.Format(ConfigHeaderFormat,
                                                     ControlInfo.Module.ModuleName,
                                                     skinData, ControlInfo.Id);

                    HeaderHtmlData = configHeader;
                    // Đặt header vào control container
                    //container.Controls.Add(new LiteralControl(configHeader));
                }
            }
        }

        protected string OnRenderDynamicControl(string html)
        {
            if (IsDesignMode && !string.IsNullOrEmpty(html) && html.IndexOf(">") >= 0)
                return html.Insert(html.IndexOf(">") + 1, HeaderHtmlData);
            return html;
        }

        protected string GetControlConfigValue(string configName)
        {
            if (null == ControlInfo || null == ControlInfo.Module) return string.Empty;

            foreach (var moduleConfiguration in ControlInfo.Configurations)
            {
                if (moduleConfiguration.ConfigName.Equals(configName, StringComparison.CurrentCultureIgnoreCase))
                {
                    return HttpContext.Current.Server.HtmlDecode(moduleConfiguration.ConfigValue);
                }
            }
            return string.Empty;
        }
    }
}
